//
//  AmusementSubViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "AmusementSubViewController.h"
#import "ImageScaleViewController.h"
@interface AmusementSubViewController ()<iCarouselDataSource,iCarouselDelegate,UIActionSheetDelegate,UMSocialUIDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) iCarousel *carousel;
@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong)  AmusementModel *model;//分享使用
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSArray *modelArray;//存放切图模式类型
@property (nonatomic, strong) UIView *modelView;
@end

@implementation AmusementSubViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
         self.wrap = YES;;
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        self.wrap = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置毛玻璃效果
    self.backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
    [self.view addSubview:self.backImage];
    
    self.carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, kP6Height(30),kWidth,kP6Height(556))];
    self.carousel.type = iCarouselTypeCoverFlow2;
    self.carousel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.carousel];
    self.carousel.dataSource = self;
    self.carousel.delegate = self;

    self.modelArray = @[@"正常",@"叠加",@"前沿",@"环绕",@"弧形",@"环形",@"层级",@"偏移",@"偏移大",@"左幻影",@"右幻影", @"左幻影大"];
    
    UIImageView *commentView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kP6Height(620), kWidth, kP6Height(47))];
    commentView.backgroundColor = [UIColor colorWithRed:0.251 green:0.000 blue:0.502 alpha:1.000];
    [self.view addSubview:commentView];
    commentView.userInteractionEnabled = YES;
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0.2)];
    lineView.backgroundColor = [UIColor blackColor];
    [commentView addSubview:lineView];
    
    UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeSystem];//返回
    leftBut.frame = CGRectMake(kP6Height(20), kP6Height(5), kP6width(35), kP6Height(37));
    [leftBut setBackgroundImage:[UIImage imageNamed:@"common_icon_return@3x"] forState:UIControlStateNormal];
    [leftBut addTarget:self action:@selector(leftButAction) forControlEvents:UIControlEventTouchUpInside];
    [commentView addSubview:leftBut];
    
    UIButton *modelBut = [UIButton buttonWithType:UIButtonTypeCustom];//观看模式
    modelBut.frame =CGRectMake(kP6width(340)/2, kP6Height(5), kP6width(35), kP6width(37));
    [modelBut setImage:[UIImage imageNamed:@"article_list@2x"] forState:UIControlStateNormal];
    [modelBut addTarget:self action:@selector(switchCarouselType) forControlEvents:UIControlEventTouchUpInside];
    [commentView addSubview:modelBut];
    
    UIButton *rightBut = [UIButton buttonWithType:UIButtonTypeSystem];//分享
    rightBut.frame = CGRectMake(kP6width(320), kP6Height(5), kP6width(35), kP6Height(37));
    [rightBut setBackgroundImage:[UIImage imageNamed:@"common_icon_share@3x"] forState:UIControlStateNormal];
    [rightBut addTarget:self action:@selector(rightButAction) forControlEvents:UIControlEventTouchUpInside];
    [commentView addSubview:rightBut];

}

#pragma mark iCarousel 方法
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.imageArr.count;//返回的个数
}

- (UIView *)carousel:(  iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    self.model = [[AmusementModel alloc] init];
    self.model = self.imageArr[index];
    AmusementModel *oneModel = self.imageArr[0];
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:oneModel.url]];
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,kP6width(300), kP6Height(200))];
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;//图片剪切模式
    if (self.model.m_url.length == 0) {
        self.imageView.image = [UIImage imageNamed:@"12345678.png"];
    }
    else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.model.url]];
    
    }
    return self.imageView;
}

////看图模式切换
- (void)switchCarouselType
{
    //模式
    self.modelView = [[UIView alloc] initWithFrame:CGRectMake(kP6width(310)/2, kP6Height(590), kP6width(120), kP6Height(30))];
    self.modelView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.modelView];
    UITableView *modelTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kP6width(120), kP6Height(30)) style:UITableViewStylePlain];
    modelTableView.backgroundColor = [UIColor clearColor];
    [self.modelView addSubview:modelTableView];
    modelTableView.delegate = self;
    modelTableView.dataSource = self;
    [modelTableView setShowsVerticalScrollIndicator:NO];
    modelTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    modelTableView.rowHeight = 30;

}

//界面动画
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0) {
        iCarouselType type = buttonIndex;
        [UIView beginAnimations:nil context:nil];
        self.carousel.type = type;
        self.navigationItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];
    }
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
    return 1;
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //3d效果
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.carousel.itemWidth);
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            return value * 1.05f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.carousel.type == iCarouselTypeCustom)
            {
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

//点击图片时触发的方法
- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    ImageScaleViewController *imageScaleVC = [[ImageScaleViewController alloc] init];
    imageScaleVC.imageArr = self.imageArr;
    imageScaleVC.index = index;
    [self.navigationController pushViewController:imageScaleVC animated:NO];
}

#pragma mark - 点击 button
- (void)leftButAction//返回
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)rightButAction//分享
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55a87dbd67e58eb9b30039ad"
                                      shareText:self.model.url
                                     shareImage:(UIImage *)self.imageView.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,nil]delegate:self];
}

#pragma mark - 模式表示图展示
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    cell.textLabel.text = self.modelArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:kP6width(14)];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;//点击没有状态
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.carousel.type = indexPath.row;
    [self.modelView removeFromSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
